// common/component/getUserInfo/getUserInfo.js
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isAuthorization: false,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    userInfoHandler(e) {
      console.log(e.detail);
      let userInfo = wx.getStorageSync('userInfo') || { wxInfo: {}};
      if (!userInfo.wxInfo) {
        userInfo.wxInfo = {};
      }
      let that = this;
      let isAuthorization = false;
      if (e.detail.errMsg === 'getUserInfo:ok') {
        isAuthorization = true;
        userInfo.wxInfo = Object.assign(userInfo.wxInfo, e.detail.userInfo);
        wx.setStorageSync('userInfo', userInfo);
        app.util.request({
          url: 'auth/session/userinfo',
          data: {
            signature: e.detail.signature,
            rawData: e.detail.rawData,
            iv: e.detail.iv,
            encryptedData: e.detail.encryptedData,
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          cachetime: 0,
          success: function (res) {
            if (!res.data.errno) {
              userInfo.memberInfo = res.data.data;
              wx.setStorageSync('userInfo', userInfo);
            }
          },
        });

        // 5.4新增点餐专用
        // if (app.siteInfo.module === 'ws_diancan_multi' || app.siteInfo.module === 'ws_diancan_single') {
        // 兼容处理餐饮进行业务模块拼接时作为副模块的问题
        let diancanJson = app.siteInfo.appJson.filter(item => item.module === 'ws_diancan_multi' || item.module === 'ws_diancan_single');
        if (diancanJson.length > 0) {
          let openid = wx.getStorageSync('openid');
          if (!openid) {
            wx.login({
              success(res) {
                let code = res.code;
                wx.setStorageSync('code', res.code);
                app.util.request({
                  url: 'auth/session/openid',
                  cachetime: '0',
                  data: {
                    code: code,
                  },
                  success(res) {
                    if (res.data.data && res.data.data.isopenqywx && app.util.isWxWork()) {
                      app.util.qyWXLogin();
                    }
                    console.log('=> 获取openid信息');
                    console.log(res);
                    wx.setStorageSync('openid', res.data.data.openid);
                    that.fireLogin(res.data.data.openid, isAuthorization, e);
                  },
                });
              },
            });
          } else {
            that.fireLogin(openid, isAuthorization, e);
          }
        } else {
          that.triggerEvent('isAuthorization', {
            isAuthorization: isAuthorization,
          }, {
            bubbles: false,
            composed: false,
            capturePhase: false,
          });
        }
      } else {
        isAuthorization = false;
        that.triggerEvent('isAuthorization', {
          isAuthorization: isAuthorization,
        },
        {
          bubbles: false,
          composed: false,
          capturePhase: false,
        });
      }
    },

    fireLogin(openid, isAuthorization, e) {
      let that = this;
          app.util.request({
            url: 'entry/wxapp/Login',
            cachetime: 0,
            data: { openid: openid, img: e.detail.userInfo.avatarUrl, name: e.detail.userInfo.nickName },
            success: function (res) {
              wx.setStorageSync('users', res.data);
              app.util.request({
                url: 'entry/wxapp/New',
                cachetime: 0,
                data: { user_id: res.data.id, store_id: getApp().sjid },
                success: function (rex) {
                  // wx.setStorageSync('new_user', rex.data)
                  wx.setStorage({
                    key: 'new_user',
                    data: rex.data,
                    success: function () {
                      that.triggerEvent('isAuthorization', {
                        isAuthorization: isAuthorization,
                      }, {
                        bubbles: false,
                        composed: false,
                        capturePhase: false,
                      });
                    },
                  });
                },
              });
            },
          });
    },
  },
});
